package com.ruoyi.system.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.Instrumentation;
import com.ruoyi.system.service.IInstrumentationService;

/**
 * 仪器Controller
 * 
 * @author malang
 * @date 2022-02-01
 */
@RestController
@RequestMapping("/labbase/instrumentation")
public class InstrumentationController extends BaseController {
	@Autowired
	private IInstrumentationService instrumentationService;

	/**
	 * 查询仪器列表
	 * 公共资源查询不设访问权限
	 */
	//@PreAuthorize("@ss.hasPermi('labbase:instrumentation:query')")
	@GetMapping("/list")
	public TableDataInfo list(Instrumentation instrumentation) {
		startPage();
		List<Instrumentation> list = instrumentationService.selectInstrumentationList(instrumentation);
		return getDataTable(list);
	}

	/**
	 * 导出仪器列表
	 */
	//@PreAuthorize("@ss.hasPermi('labbase:instrumentation:export')")
	@Log(title = "仪器", businessType = BusinessType.EXPORT)
	@GetMapping("/export")
	public AjaxResult export(Instrumentation instrumentation) {
		List<Instrumentation> list = instrumentationService.selectInstrumentationList(instrumentation);
		ExcelUtil<Instrumentation> util = new ExcelUtil<Instrumentation>(Instrumentation.class);
		return util.exportExcel(list, "instrumentation");
	}

	/**
	 * 获取仪器详细信息
	 * 公共资源查询不设访问权限
	 */
	//@PreAuthorize("@ss.hasPermi('labbase:instrumentation:query')")
	@GetMapping(value = "/{instrumentationId}")
	public AjaxResult getInfo(@PathVariable("instrumentationId") Long instrumentationId) {
		return AjaxResult.success(instrumentationService.selectInstrumentationById(instrumentationId));
	}
	
	/**
	 * 获取仪器详细信息
	 */
	//@PreAuthorize("@ss.hasPermi('labbase:instrumentation:query')")
	@GetMapping(value = "/byno/{assetNo}")
	public AjaxResult getDeviceByno(@PathVariable("assetNo") String assetNo) {
		Instrumentation instrumentation = new Instrumentation();
		instrumentation.setAssetNo(assetNo);
		List<Instrumentation> list = instrumentationService.selectInstrumentationList(instrumentation);
		if(list.size()>0){
			instrumentation = list.get(0);
		}
		return AjaxResult.success(instrumentation);
	}

	/**
	 * 新增仪器
	 */
	@PreAuthorize("@ss.hasPermi('labbase:instrumentation:edit')")
	@Log(title = "仪器", businessType = BusinessType.INSERT)
	@PostMapping
	public AjaxResult add(@RequestBody Instrumentation instrumentation) {
		Instrumentation query = new Instrumentation();
		query.setAssetNo(instrumentation.getAssetNo());
		List<Instrumentation> list = instrumentationService.selectInstrumentationList(query);
		if(list.size()>0){
			return  AjaxResult.error("该资产编号已经存在，请检查！");
		}
		int key = instrumentationService.insertInstrumentation(instrumentation);
		if(key==0){
			return  AjaxResult.error("该资产编号已经存在，请检查！");
		}
		return toAjax(key);
	}

	/**
	 * 修改仪器
	 */
	@PreAuthorize("@ss.hasPermi('labbase:instrumentation:edit')")
	@Log(title = "仪器", businessType = BusinessType.UPDATE)
	@PutMapping
	public AjaxResult edit(@RequestBody Instrumentation instrumentation) {
		return toAjax(instrumentationService.updateInstrumentation(instrumentation));
	}

	/**
	 * 删除仪器
	 */
	@PreAuthorize("@ss.hasPermi('labbase:instrumentation:edit')")
	@Log(title = "仪器", businessType = BusinessType.DELETE)
	@DeleteMapping("/{instrumentationIds}")
	public AjaxResult remove(@PathVariable Long[] instrumentationIds) {
		return toAjax(instrumentationService.deleteInstrumentationByIds(instrumentationIds));
	}
}
